package com.example.demo.controller;

import com.example.demo.dto.CdcMessage;
import com.example.demo.kafka.CdcMessageProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/api/cdc")
public class CdcMessageController {

    @Autowired
    private CdcMessageProducer cdcMessageProducer;

    @PostMapping("/messages")
    public ResponseEntity<String> sendCdcMessage(@RequestBody HashMap message) {
        try {
            cdcMessageProducer.sendCdcMessage(message);
            return ResponseEntity.ok("CDC消息已发送到Kafka");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("发送消息失败: " + e.getMessage());
        }
    }
}    